package com.yunshouhu.pkid;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 判断是哪一家加固
 *
 *
 */
class CheckEncryptionBean {

    private int flag;

    private String companyName;
    
    private String feature;

    public CheckEncryptionBean() {
        flag = -1;
        companyName="";
        feature="";
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getFeature() {
        return feature;
    }

    public void setFeature(String feature) {
        this.feature = feature;
    }
    

}

public class CheckEncryptionCompany {

    //public static Map<Integer, CheckEncryptionBean> map = new HashMap<Integer, CheckEncryptionBean>();
    public static Map<String, CheckEncryptionBean> map = new HashMap<String, CheckEncryptionBean>();
    public static final int FLAG_Bancle = 0;
    public static final int FLAG_360 = 1;
    public static final int FLAG_tongfudun = 2;
    public static final int FLAG_wangqing = 3;
    public static final int FLAG_TX = 4;
    public static final int FLAG_NAGA = 5;
    public static final int FLAG_ali = 6;
    public static final int FLAG_baidu = 7;
    public static final int FLAG_ijm = 8;

    public static final int FLAG_wangyi = 9;//网易云加密
    public static final int FLAG_apkProtect = 10;//apkProtect加密

    public static CheckEncryptionBean getCompany(String apkPath, String projectPath) {
       if(map.size()==0)
       { 
           getmap(projectPath);          
       }
       return seearchFile(apkPath);

    }

    /**
     * 读取apk内文件路径
     *
     * @param apkPath
     * @return
     */
    private static List<String> readApks(String apkPath) {
        List<String> strArr = new ArrayList<String>();
        try {
            ZipInputStream Zin = new ZipInputStream(
                    new FileInputStream(apkPath));// 输入源zip路径
            ZipEntry entry;
            try {
                while ((entry = Zin.getNextEntry()) != null) {
                    strArr.add(entry.getName());
                }
                if (Zin != null) {
                    Zin.closeEntry();
                    Zin.close();
                    Zin = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return strArr;
    }
    
    private static CheckEncryptionBean seearchFile(String apkPath) {
        List<String> filelist = readApks(apkPath);
        for (int i = 0; i < filelist.size(); i++) {
            String filePath = filelist.get(i);
            if (!filePath.equals("")) {
                for (String feature : map.keySet()) {
                   if (filePath.matches(feature))  {
                        CheckEncryptionBean bean = map.get(feature);
                        return bean;
                    }
                }
            }
        }
        return new CheckEncryptionBean();
    }

    // private int seearchFile(List<String> filelist,int result) {
    // File[] fm = dir.listFiles();
    // for (File file : fm) {
    // if (file.isDirectory()) {
    // result=seearchFile(file,result);
    // } else if (map.containsKey(file.getName())) {
    // result=map.get(file.getName()).getFlag();
    // return result;
    // }
    // }
    // return result;
    // }
    private static void getmap(String projectPath) {
        File cachePath = new File(projectPath);

        if (!cachePath.exists()) {
            new Exception("temp文件夹不存在!!!").printStackTrace();
            return;
        }
        cachePath = new File(cachePath.getAbsoluteFile(), "Reinforcement.xml");
        if (!cachePath.exists()) {
            new Exception("无法找到指定的Reinforcement.xml文件" + cachePath.toString())
                    .printStackTrace();
        }

        try {
            String xmlContent = Tool.reader(cachePath.getAbsolutePath());
            Document document = Tool.xmlAnalysis(xmlContent);
            NodeList nList = document.getChildNodes().item(0).getChildNodes();
            if (nList == null || nList.getLength() <= 0) {
                new Exception("解析xml数据异常!!").printStackTrace();
            }
            getRelNode(nList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    /**
     * 解析xml
     *
     * @param nodeList
     */
    private static void getRelNode(NodeList nodeList) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == 1) {
                NamedNodeMap nodeMap = node.getAttributes();
                if (xmlCheck(nodeMap, "feature") && xmlCheck(nodeMap, "name")
                        && xmlCheck(nodeMap, "flag")) {
                    String feature = nodeMap.getNamedItem("feature")
                            .getNodeValue();
                    String name = nodeMap.getNamedItem("name").getNodeValue();
                    int flag = Integer.parseInt(nodeMap.getNamedItem("flag")
                            .getNodeValue());
                    CheckEncryptionBean bean = new CheckEncryptionBean();
                    bean.setCompanyName(name);
                    bean.setFlag(flag);
                    map.put(feature, bean);
                }
            } else {
                getRelNode(node.getChildNodes());
            }
        }
    }

    /**
     * xml验证
     *
     * @param item
     * @return
     */
    private static boolean xmlCheck(NamedNodeMap nodeMap, String item) {
        Node node = nodeMap.getNamedItem(item);
        if (node == null) {
            new Exception("解析xml的" + item + "时发生异常!!").printStackTrace();
        }
        return node != null;
    }
}
